<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="1500" destroy-on-close :close-on-click-modal="false" :close-on-press-escape="mode != 'show' ? false : true" @closed="$emit('closed')" align-center>
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" label-width="130" ref="dialogForm">
            <el-row :gutter="1">
                <el-col :span="6">
                    <el-form-item label="入入仓单号/唛头" prop="entry_code">
                        <el-input v-model="form.entry_code" placeholder="入入仓单号/唛头" clearable></el-input>
                    </el-form-item>
                </el-col>
                <!-- <el-col :span="6">
                    <el-form-item label="名称" prop="other_name">
                        <el-input v-model="form.other_name" placeholder="请输入名称" clearable></el-input>
                    </el-form-item>
                </el-col> -->
                <el-col :span="6">
                    <el-form-item label="日期" prop="entry_date">
                        <el-date-picker v-model="form.entry_date" type="date" placeholder="请选择日期" :value-format="'YYYY-MM-DD'" style="width:100%" />
                    </el-form-item>
                </el-col>
                <el-col :span="6">
                    <el-form-item label="调入仓库" prop="ware_id">
                        <el-select v-model="form.ware_id" filterable style="width: 100%" placeholder="仓库" @change="wareChange" clearable disabled>
                            <el-option v-for="item in wareList" :key="item.id" :label="item.ware_name" :value="item.id" />
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-col :span="12">
                <el-form-item label="备注" prop="remark">
                    <el-input v-model="form.remark" placeholder="请输入备注" type="textarea" clearable></el-input>
                </el-form-item>
            </el-col>
            <el-form-item label="调箱产品清单" prop="detail">
                <el-button type="primary" size="small" icon="el-icon-plus" @click="checkShop" v-if="is_order">选择产品</el-button>
                <sc-form-table ref="table" v-model="form.detail" :addTemplate="addTemplate" placeholder="暂无数据" height="400" hideAdd show-summary>
                    <el-table-column label="入仓单号/唛头" align="center" width="180">
                        <template #default="scope">
                            {{scope.row.marking}}
                        </template>
                    </el-table-column>
                    <el-table-column label="箱号" align="center" width="80">
                        <template #default="scope">
                            {{scope.row.box_code}}
                        </template>
                    </el-table-column>
                    <el-table-column label="品类" align="center" width="100">
                        <template #default="scope">
                            {{scope.row.cate_name}}
                        </template>
                    </el-table-column>
                    <el-table-column label="商品型号" align="center">
                        <template #default="scope">
                            {{scope.row.goods_name}}
                        </template>
                    </el-table-column>
                    <!-- <el-table-column label="品名" align="center">
                        <template #default="scope">
                            {{scope.row.goods_name}}
                        </template>
                    </el-table-column> -->
                    <el-table-column label="采购数量" align="center" prop="produce_number" width="100"></el-table-column>
                    <!-- <el-table-column label="已调箱数量" align="center" prop="all_number" width="100"></el-table-column>
                    <el-table-column label="剩余可调箱数量" align="center" prop="surplus_number" width="110"></el-table-column>
                    <el-table-column label="本次调箱数量" prop="number" align="center" width="100">
                        <template #default="scope">
                            <el-input v-model="scope.row.number" placeholder="数量" @input="count(scope.row)" @focus="onFocus($event)"></el-input>
                        </template>
                    </el-table-column> -->
                    <el-table-column label="调出仓库" align="center">
                        <template #default="scope">
                            <el-select v-model="scope.row.out_ware_id" disabled style="width: 100%" placeholder="仓库">
                                <el-option v-for="item in wareList" :key="item.id" :label="item.ware_name" :value="item.id" />
                            </el-select>
                        </template>
                    </el-table-column>
                    <el-table-column label="调入仓库" align="center">
                        <template #default="scope">
                            <el-select v-model="scope.row.ware_id" disabled style="width: 100%" placeholder="仓库">
                                <el-option v-for="item in wareList" :key="item.id" :label="item.ware_name" :value="item.id" />
                            </el-select>
                        </template>
                    </el-table-column>
                    <el-table-column label="备注" prop="remark" align="center">
                        <template #default="scope">
                            <el-input v-model="scope.row.remark" placeholder="备注"></el-input>
                        </template>
                    </el-table-column>
                </sc-form-table>
            </el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false" >取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit(0)">保 存</el-button>
			<el-button v-if="mode == 'add' || mode == 'ware'" type="primary" :loading="isSaveing" @click="submit(1)">保存并审核</el-button>
		</template>
	</el-dialog>
    <!-- <select-shoporder v-if="dialog.shop" ref="shopDialog" @success="shopUpdate" @closed="dialog.shop=false" :query="{status: 1,move_ware: 1}"></select-shoporder> -->
    <select-shoporder v-if="dialog.shop" ref="shopDialog" @success="shopUpdate" @closed="dialog.shop=false" :query="{status: 1,is_all_transfer: 0}"></select-shoporder>
</template>

<script>
import SelectShoporder from '../order/selectShoporder.vue';
	export default {
    components: { SelectShoporder },
		emits: ['success', 'closed'],
		data() {
			return {
				mode: "add",
				titleMap: {
					add: '新增',
					ware: '新增',
					edit: '编辑',
					show: '查看',
				},
				visible: false,
				isSaveing: false,
				form: {
					id:"",
					entry_code: "",
					entry_date: this.moment().format('YYYY-MM-DD'),
					ware_id: "",
					remark: "",
                    detail:[],
                    is_check: 0,
				},
                rules: {
                    entry_code: [
                        { required: true, message: '请输入入仓单号/唛头' }
                    ],
                },
                addTemplate:{},
                dialog: {
                    shop: false,
                },
                wareList:[],
                ware_id: "",
                is_order: true,
			}
		},
		mounted() {
            this.getWare()
		},
		methods: {
			//显示
			open(mode='add'){
				this.mode = mode;
				this.visible = true;
				return this
			},
			//表单提交方法
			submit(e){
                this.form.is_check = e
                this.$refs.dialogForm.validate(async (valid) => {
                    if (valid) {
                        this.isSaveing = true;
                        var res  = [];
                        if (this.form.id) {
                            res = await this.$API.procure.check.edit.post(this.form);
                        }else{
                            res = await this.$API.procure.check.add.post(this.form);
                        }
                        this.isSaveing = false;
                        if(res!=''){
                            if(res.code == 1){
                                this.$emit('success', this.form, this.mode)
                                this.visible = false;
                                this.$message.success("操作成功")
                            }else{
                                this.$alert(res.message, "提示", {type: 'error'})
                            }
                        }
                    }
                })
			},
			//表单注入数据
			async setData(data){
				this.form.id = data.id
                var res = await this.$API.procure.check.detail.get({id:data.id});
                this.form.entry_code = res.data.entry_code
                this.form.entry_date = res.data.entry_date
                this.form.ware_id = res.data.ware_id
                this.form.remark = res.data.remark
                let array = []
                res.data.detail.map(item=>{
                    array.push({
                        id:item.id,
                        goods_id:item.goods_id,
                        purchase_detail_id:item.purchase_detail_id,
                        marking:item.detail && item.detail.marking,
                        box_code:item.detail && item.detail.box_code,
                        goods_model:item.detail && item.detail.goods && item.detail.goods.goods_model,
                        goods_name:item.detail && item.detail.goods && item.detail.goods.goods_name,
                        cate_name:item.detail && item.detail.goods && item.detail.goods.cate && item.detail.goods.cate.cate_name,
                        // all_number:item.detail && item.detail.all_number,
                        produce_number:item.detail && item.detail.number,
                        out_ware_id:item.detail && item.detail.ware_id,
                        // surplus_number:item.detail && item.detail.number - item.detail.all_number,
                        number:item.number,
                        ware_id:item.ware_id,
                        remark:item.remark,
                    })
                })
                this.form.detail = array
			},
            // 选择产品
            checkShop(){
                this.dialog.shop = true
                this.$nextTick(() => {
                    this.$refs.shopDialog.open('add')
                })
            },
            // 选择产品返回
            shopUpdate(data){
                console.log('data :>> ', data);
                let array = []
                data.map((item)=>{
                    array.push({
                        // goods_id:item.goods_id,
                        // purchase_detail_id:item.id,
                        // marking:item.marking,
                        // box_code:item.box_code,
                        // cate_name:item.cate_name,
                        // goods_name:item.goods_name,
                        // produce_number:item.number,
                        // number:item.number,
                        // ware_id:this.form.ware_id,
                        // out_ware_id: item.ware_id
                        goods_id:item.goods_id,
                        purchase_detail_id:item.id,
                        marking:item.marking,
                        box_code:item.box_code,
                        cate_name:item.goods && item.goods.cate && item.goods.cate.cate_name,
                        goods_name:item.goods && item.goods.goods_name,
                        produce_number:item.number,
                        number:item.number,
                        ware_id:this.form.ware_id,
                        out_ware_id: item.ware_id
                    })
                })
                let existingIds = this.form.detail.map(item => item.goods_id);
                for(let i = array.length - 1; i >= 0; i--){
                    if(existingIds.includes(array[i].goods_id)){
                        array.splice(i, 1);
                    }
                }
                this.form.detail = [...this.form.detail,...array]
            },
            onFocus(event){
                event.target.select();
            },
            // 获取仓库
            async getWare(){
                var res = await this.$API.goods.ware.all.get();
                this.wareList = res.data;
                this.form.ware_id = 1
            },
            wareChange(e){
                this.form.detail.map(item => item.ware_id = e)
            },
            count(row){
                if(row.number > row.surplus_number){
                    this.$message.error("本次调箱数量不能超过剩余可调箱数量")
                    row.number = row.surplus_number
                }
            },
            async comeOrder(data){
                this.is_order = false
                var res = await this.$API.procure.order.detail.get({id:data.id});
                let array = []
                res.data.detail.map(item=>{
                    if(item.move == 0){
                        array.push({
                            goods_id:item.goods_id,
                            purchase_detail_id:item.id,
                            marking:item.marking,
                            box_code:item.box_code,
                            goods_name:item.goods && item.goods.goods_name,
                            cate_name:item.goods && item.goods.cate && item.goods.cate.cate_name,
                            produce_number:item.number,
                            // all_number:item.all_number,
                            // surplus_number:item.number - item.all_number,
                            number:item.number,
                            out_ware_id:item.ware_id,
                            ware_id:this.form.ware_id,
                        })
                    }
                })
                this.form.detail = array
            },
		}
	}
</script>

<style>
</style>
